from flask import jsonify
import json
import bcrypt
from sqlalchemy import inspect
from routes import app, db

# 读取 JSON 文件
def load_data():
    with open('./static/json/shangluoModelData.json', 'r', encoding='utf-8') as file:
        return json.load(file)

@app.route('/data', methods=['GET'])
def get_data():
    data = load_data()
    print(data)
    return jsonify(data)

@app.route('/dataDev', methods=['GET'])
def get_dataDev():
    data = {
        "posts": [
            {"id": 1, "title": "Hello World!Hello World"}
        ],
        "comments": [
            {"id": 1, "body": "Nice post!", "postId": 1}
        ]
    }
    return jsonify(data)

@app.route('/')
def hello_world():  # put application's code here
    return '商洛市项目的后端数据服务 !'

def init_db():
    with app.app_context():
        inspector = inspect(db.engine)
        if not inspector.has_table('users'):
            from models.user import User
            db.create_all()
            password_hashed = bcrypt.hashpw('123456'.encode(), bcrypt.gensalt())
            user = User(username="root", password=password_hashed.decode('utf-8'), fullname='root', description='')
            db.session.add(user)
            db.session.commit()

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8030)








